package com.bridge.applets.controller;


import com.bridge.applets.model.vo.BodyList;
import com.bridge.applets.model.vo.PageBodyList;
import com.bridge.applets.model.vo.req.*;
import com.bridge.applets.model.vo.resp.BridgeCheckMeasureModelNumResp;
import com.bridge.applets.model.vo.resp.BridgeMeasureModelSerialNumResp;
import com.bridge.applets.model.vo.resp.BridgeMeasureResp;
import com.bridge.applets.model.vo.resp.BridgeRecordResp;
import com.bridge.applets.service.BridgeMobileService;
import com.bridge.applets.service.BridgePcService;
import com.bridge.applets.utils.RedisLockUtil;
import com.bridge.common.consts.RedisKeyEnum;
import com.bridge.common.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xyjian
 * @since 2024-11-06
 */
@RestController
@RequestMapping("/pc/bridge")
public class BridgePcController {

    @Autowired
    private BridgePcService bridgePcService;

    @Autowired
    private BridgeMobileService bridgeMobileService;
    @Autowired
    private RedisLockUtil redisLockUtil;

    @PostMapping("/add")
    public R<Void> addBridge(@RequestBody BridgeAddReq req) {
        bridgePcService.addBridge(req);
        return R.success();
    }

    @PostMapping("/analysis")
    public R<BridgeRecordResp> analysisBridge(@RequestBody BridgeAnalysisReq req) {
        BridgeRecordResp resp = redisLockUtil.executeReturnNoLockThrowException(RedisKeyEnum.ANALYSIS_LOCK.genKey(req.getRecordId()),
                "桥梁正在分析中，请等待处理完成",
                () -> {
                    return bridgePcService.analysisBridge(req);
                }
        );
        return R.success(resp);
    }

    @PostMapping("/listByPage")
    public R<PageBodyList<BridgeRecordResp>> bridgeListByPage(@RequestBody BridgeRecodeListReq param) {
        PageBodyList<BridgeRecordResp> resp = bridgePcService.bridgeListByPage(param);
        return R.success(resp);
    }


    @PostMapping("/checkMeasureModelNum")
    public R<BridgeCheckMeasureModelNumResp> checkMeasureModelNum(@RequestBody BridgeCheckMeasureModelNumReq req) {
        BridgeCheckMeasureModelNumResp resp = bridgePcService.checkMeasureModelNum(req);
        return R.success(resp);
    }

    /**
     * 删除桥梁记录
     *
     * @param req
     * @return
     */
    @PostMapping("/delete")
    public R<Void> deleteBridge(@RequestBody BridgeDeleteReq req) {
        bridgePcService.deleteBridge(req);
        return R.success();
    }

    /**
     * 重置分析结果
     *
     * @param req
     * @return
     */
    @PostMapping("/resetResult")
    public R<Void> resetResult(@RequestBody BridgeResetResultReq req) {
        bridgePcService.resetResult(req);
        return R.success();
    }

    /**
     * 获取桥梁样本数据列表
     *
     * @param req
     * @return
     */
    @PostMapping("/measure/listAll")
    public R<BodyList<BridgeMeasureResp>> bridgeMeasureListByPage(@RequestBody BridgeMeasureReq req) {
        BodyList<BridgeMeasureResp> resp = bridgePcService.bridgeMeasureListAll(req);
        return R.success(resp);
    }

    /**
     * 获取桥梁样本编码序列号
     * @param req
     * @return
     */
    @PostMapping("/getMeasureFileSerialNum")
    public R<BridgeMeasureModelSerialNumResp> getMeasureFileSerialNum(@RequestBody BridgeMeasureFileSerialNumReq req) {
        BridgeMeasureModelSerialNumResp resp = bridgeMobileService.getMeasureFileSerialNum(req);
        return R.success(resp);
    }

    /**
     * 添加测量样本数据
     * @param file
     * @param fileName
     * @param bridgeName
     * @param bridgeCode
     * @param sampleType
     * @return
     */
    @PostMapping("/addMeasureModel")
    public R<Void> addMeasureModel(@RequestParam(name = "file") MultipartFile file, String fileName, String bridgeName, String bridgeCode,Integer sampleType) {
        bridgeMobileService.addMeasureModel(file, fileName, bridgeName, bridgeCode,sampleType,"pc");
        return R.success();
    }


    /**
     * 桥梁详情
     *
     * @param req
     * @return
     */
    @PostMapping("/detail")
    public R<BridgeRecordResp> detailBridge(@RequestBody BridgeDetailReq req) {
        BridgeRecordResp resp= bridgePcService.detailBridge(req);
        return R.success(resp);
    }
}
